<?php
/**
 *
 *精彩瞬间控制器
 *
 **/

namespace Admin\Controller;

class WonderfulMomentController extends ComController
{

    /*
     * 添加精彩瞬间
     */
    public   function  add(){

        $uid = session('uid');
        $member = M('member');
        $user= $member->where('uid ='.$uid)->find();

        if($user['level'] == 1){

            $sc_id=$uid;

        }else if($user['level']  ==2){

            $sc_id = $user['pid'];

        }else{

            echo '';

        }

        if(IS_POST){

                $class_id =   I('post.class_type') ? I('post.class_type') : '';
                $title =   I('post.title') ? I('post.title') : '';
                $content =   I('post.content') ? I('post.content') : '';
                $picture =   I('post.picture') ? I('post.picture') : '';

                if(!$class_id) $this->error('请选择班级类型');
                if(!$title) $this->error('请填写精彩瞬间标题');
                if(!$content) $this->error('请填写精彩瞬间内容');
                if(!$picture) $this->error('请上传精彩瞬间照片');

                $data['class_id']=$class_id;
                $data['title']=$title;
                $data['content']=$content;
                $data['picture']=$picture;
                $data['sc_id']=$sc_id;
                $data['add_time']=time();
                $data['update_time']=time();

                $wonderful_moment = M('wonderful_moment');
                $result= $wonderful_moment->add($data);

                if($result){
                    $this->success('添加精彩瞬间成功！',U('index'));
                }else{
                    $this->error('添加精彩瞬间失败!');
                }

        }else{

             $class = M('class');
             $class_type = $class->where(array('sc_id'=>$sc_id,'is_del'=>1))->select();

             $this->assign('class_type',$class_type);
             $this->display();
        }

    }


    /*
     *精彩瞬间列表
     */
    public   function  index(){

        $uid = session('uid');
        $member = M('member');
        $user= $member->where('uid ='.$uid)->find();

        if($user['level'] == 1){

            $sc_id=$uid;

        }else if($user['level']  ==2){

            $sc_id = $user['pid'];

        }else{

            echo '';

        }

        //搜索分页
        $p= intval($_GET['p'])?$_GET['p']:1;
        $wonderful_moment= M('wonderful_moment');
        $pagesize = 2;#每页数量
        $offset = $pagesize * ($p - 1);//计算记录偏移量
        $prefix = C('DB_PREFIX');

        $where = 'w.sc_id ='. $sc_id;

        //精彩瞬间标题
        $title = isset($_GET['title']) ? htmlentities($_GET['title']) : '';
        //班级类型
        $class_type = isset($_GET['class_type']) ? htmlentities($_GET['class_type']) : '';

        if ($title) {
            $where .= "  and  w.title like '%{$title}%' ";
        }

        if ($class_type) {
            $where .= "  and  c.id = " .$class_type;
        }

        $data= $wonderful_moment->join('as w left join  sc_class   as c   on w.class_id=c.id')
            ->field('w.id, w.title, w.content, w.picture, w.add_time, c.name')
            ->where($where)
            ->order('w.id asc' )
            ->limit($offset . ',' . $pagesize)
            ->select();

        $count= $wonderful_moment->join('as w left join  sc_class   as c   on w.class_id=c.id')
            ->field('w.id, w.title, w.content, w.picture, w.add_time, c.name')
            ->where($where)
            ->order('w.id asc' )
            ->limit($offset . ',' . $pagesize)
            ->count();

        $class = M('class');
        $class_type= $class->where(array('sc_id'=>$sc_id,'is_del'=>1))->select();

        $page = new \Think\Page($count, $pagesize);
        $page = $page->show();

        $this->assign('data',$data);
        $this->assign('page', $page);
        $this->assign('class_type', $class_type);
        $this->display();

    }


    /*
     * 修改精彩瞬间
     */
    public  function    edit (){

        $uid = session('uid');
        $member = M('member');
        $user= $member->where('uid ='.$uid)->find();

        if($user['level'] == 1){

            $sc_id=$uid;

        }else if($user['level']  ==2){

            $sc_id = $user['pid'];

        }else{

            echo '';

        }

        $id = I('get.id') ? I('get.id') : 0;

        if(IS_POST){

                $wonderful_moment = M('wonderful_moment');
                $res = $wonderful_moment->where('id ='.$id  and 'sc_id ='.$sc_id)->find();

                if($res){

                    $param['class_id'] =  I('post.class_type');
                    $param['title'] =  I('post.title');
                    $param['content'] =  I('post.content');
                    $param['picture'] =  I('post.picture');
                    $param['update_time'] = time();

                    $wonderful_moment = M('wonderful_moment');
                    $wonderful_moment->where(array('id'=>$id,'sc_id'=>$sc_id))->save($param);


                    $this->success('修改精彩瞬间成功！',U('index'));die;

                }else{

                    $this->error('修改精彩瞬间失败，参数错误！');

                }

        }else{

            $wonderful_moment = M('wonderful_moment');
            $data= $wonderful_moment->join('as w left join  sc_class   as c   on w.class_id=c.id')
                ->field('w.id, w.title, w.content, w.picture, w.add_time, w.class_id, c.name')
                ->where('w.id ='.$id)
                ->find();

            $class = M('class');
            $class_content = $class->where(array('sc_id'=>$sc_id,'is_del'=>1))->select();

            $this->assign('data',$data);
            $this->assign('class_content',$class_content);
            $this->display();

        }

    }


    /*
   * 删除精彩瞬间
   */
    public  function  del(){

        $uid = session('uid');
        $member = M('member');
        $user= $member->where('uid ='.$uid)->find();

        if($user['level'] == 1){

            $sc_id=$uid;

        }else if($user['level']  ==2){

            $sc_id = $user['pid'];

        }else{

            echo '';

        }

        $id = I('get.id') ? I('get.id') : 0;

        $wonderful_moment = M('wonderful_moment');
        $data = $wonderful_moment->where('id ='.$id  and 'sc_id ='.$sc_id)->find();

        if($data){

                $res = $wonderful_moment->where(array('id'=>$id,'sc_id'=>$sc_id))->delete();

                if($res){
                    $this->success('删除精彩瞬间，成功!',U('index'));
                }else{
                    $this->error('删除精彩瞬间，失败!'.$wonderful_moment->getError());
                }

        }else{

            $this->error('删除精彩瞬间失败，没有找到该记录，参数错误！');

        }

    }




}